<?php
/**
 * Description of saevcode
 * SAE的验证码类仿真
 * @author https://gitee.com/foglede
 */
class SaeVCode {
    private $vcode = '';
    private $errno = 0;
    const fontname = 'SAE_Font_MicroHei';
    const baseurl = 'http://127.0.0.1/mahuohuo/mahuohuovc.php';

    public function __construct($options = array()) {
        $this->vcode = substr(uniqid(),-4,4) ;
        $im = imagecreate(120, 30);
        imagesavealpha($im, true);
        imagealphablending($im, true);
        for ($i = 0; $i < strlen($this->vcode); $i++) {//https://gitee.com/foglede 乱七八糟大杂烩
            //$bgcolor = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            //imageellipse($im,mt_rand(8,20), mt_rand(8,20), rand(0, 100), rand(0, 100), $bgcolor);
            //imageline($im, rand(0, 80), rand(0, 80), rand(0, 80), rand(0, 80), ImageColorTransparent($im, $bgcolor));
            //imagesetpixel($im, rand(0, 100), rand(0, 100), $bgcolor);
            //imageArc($im, mt_rand(8,20), mt_rand(8,20), mt_rand(10,22), mt_rand(10,22), 5, 5, $bgcolor);
            imagettftext($im, 20, rand(0, 30), $i * 28, 25, imagecolorallocatealpha($im, mt_rand(0, 130), mt_rand(0, 130), mt_rand(0, 130), rand(30, 90)), constant(self::fontname), ' ' . (rand(0, 1) ? $this->vcode{$i} : strtoupper($this->vcode{$i})) );
            //imagefill($im, rand(0, 80),rand(0, 80), ImageColorTransparent($im, imagecolorallocatealpha($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), rand(30, 90)) ));
        }
        ob_start();
        imagepng($im);
        //$options = wincache_file_get_contents('php://output');
        $options = ob_get_contents();
        ob_end_clean();
        if(!wincache_ucache_set ('vcode' , $options ,  15)){
            $this->errno = 403;
        }
        ImageDestroy($im);
        unset ($options);
    }
//    public function __destruct(){
//        wincache_ucache_delete('vcode');
//    }
    
    public function errmsg (){
        return $this->errno==403 ? '权限不足或超出配额（在本地SAE模拟环境下，导致该错误极有可能修改了PHP.ini配置所致。）' :'放心啦，<a href="https://gitee.com/foglede" onMouseOver="javascript:void()">mahuohuo.com</a> 制作的本地SAE仿真类不会出错的啦！';
    }
    public function errno (){
        return $this->errno;
    }
    public function answer(){
        return $this->vcode;
    }
    public function question(){
        return array(
            'img_url'=> self::baseurl,
            'img_html'=>'<img src="'.self::baseurl.'" />');
    }
}

?>
